package com.example.android_raspberry;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    //控件变量
    Button bt_on;
    Button bt_off;
    Button bt_connect;
    EditText edit_ip;
    EditText edit_port, edit_rev;
    ClientThread clientThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findview();
        setlistener();

    }

    //链接Server
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg)
        {
            edit_rev.setText(msg.obj.toString());
            Log.i("1", edit_rev.getText().toString());
        }
    };

    //控件初始化
    private void findview() {

        bt_on = findViewById(R.id.button_on);
        bt_off = findViewById(R.id.button_off);
        bt_connect = findViewById(R.id.bt_connect);
        edit_ip = findViewById(R.id.edit_ip);
        edit_port = findViewById(R.id.edit_port);
        edit_rev = findViewById(R.id.rev_msg);
    }


    private void setlistener() {

        bt_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edit_ip.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this, "请先设置IP地址以及端口号", Toast.LENGTH_SHORT).show();
                }
                else {
                    String msg_ip = edit_ip.getText().toString();
                    int msg_port = Integer.parseInt(edit_port.getText().toString());

                    clientThread = new ClientThread(handler, msg_ip, msg_port);
                    //客户端启动ClientThread线程创建网络链接，读取来自服务器的数据
                    new Thread(clientThread).start();
                    if (clientThread !=null){
                        Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        bt_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    Message msg = new Message();
                    msg.obj = "1";
                    clientThread.revhandler.sendMessage(msg);
                    Log.d("send",msg.obj.toString());
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

        bt_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    Message msg = new Message();
                    msg.obj = "0";
                    clientThread.revhandler.sendMessage(msg);
                    Log.d("send", (String) msg.obj);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
}
